<?php
declare(strict_types=1);

namespace App\Controller\Admin;

use App\Service\PermissionService;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;

class PermissionController extends AbstractController
{
	/**
	 * @Inject
	 * @var PermissionService
	 */
	protected $permissionService;

	public function index()
	{
		return $this->render->render('admin/permission/index.twig');
	}

	public function create()
	{
		$permissions = $this->permissionService->queryParents();

		return $this->render->render('admin/permission/create.twig', [
			'permissions' => $permissions,
		]);
	}

	public function store()
	{
		$requestData = $this->request->post();

		$res = $this->permissionService->store($requestData);

		return $this->response->json($res);
	}

	public function edit()
	{
		$id = $this->request->route('id');

		$permission = $this->permissionService->queryById($id);
		$permissions = $this->permissionService->queryParents($id);

		return $this->render->render('admin/permission/info.twig', [
			'permission' => $permission,
			'permissions' => $permissions,
		]);
	}

	public function update()
	{
		$id = $this->request->route('id');
	}

	public function del()
	{
		$id = $this->request->route('id');
	}

	public function menus()
	{
		$permissions = $this->permissionService->queryAll();

		return $this->response->json([
			"code" => 0,
			"msg" => "",
			"count" => count($permissions),
			'data' => $permissions,
		]);
	}
}